package com.yulebaby.callsenter.task;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.lzy.okgo.model.HttpParams;
import com.yulebaby.callsenter.bean.CustomerBean;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.http.HttpHelper;
import com.yulebaby.callsenter.http.ServerUrls;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.ui.activity.LoginActivity;
import com.yulebaby.callsenter.utils.SPUtil;
import com.yulebaby.callsenter.utils.ToastUtil;

/**
 * Author: Zhangbo
 * Time: 2016/12/8
 */
public class RequestPreSaleRemarkTask {

    /**
     * 获取售前跟踪记录
     *
     * @param activity     获取信息的Acitivty
     * @param httpCallback 回调
     */
    public static void doRequestPreSaleRemarkTask(Activity activity, CustomerBean bean, HttpCallback httpCallback) {
        HttpParams httpParams = new HttpParams();
        String user_token = (String) SPUtil.get(Constant.USER_TOKEN, "");
        httpParams.put("token", user_token);
        httpParams.put("customer_id", bean.getCustomer_id());
        if (checkNull(user_token, activity)) {
            HttpHelper.getInstance().get(activity, ServerUrls.OPT_SEARCHPRESALESTRACE, httpParams, httpCallback);
        }
    }

    /**
     * 获取跟踪进展
     *
     * @param activity     获取信息的Acitivty
     * @param httpCallback 回调
     */
    public static void doRequestTrackProgressTask(Activity activity, long customerId, int visitTimes, HttpCallback httpCallback) {
        HttpParams httpParams = new HttpParams();
        String user_token = (String) SPUtil.get(Constant.USER_TOKEN, "");
        int user_id = (int) SPUtil.get(Constant.USER_ID, -1);
        if (checkNull(user_token, activity)) {
            httpParams.put("token", user_token);
            httpParams.put("sale_id", user_id);
            httpParams.put("customer_id", customerId);
            httpParams.put("visitTimes", visitTimes);
            HttpHelper.getInstance().get(activity, ServerUrls.OPT_FINDTRACKPROGRESS, httpParams, httpCallback);
        }
    }


    /**
     * 判断是否为空
     *
     * @param string
     * @return
     */
    private static boolean checkNull(String string, Activity mActivity) {
        if (TextUtils.isEmpty(string)) {
            ToastUtil.showToast("登录信息出错,请重新登录");
            //TOKEN 失效跳转到登录界面
            SPUtil.remove(Constant.USER_TOKEN); //对登录信息进行清除
            SPUtil.remove(Constant.USER_ID);
            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
            mActivity.finish();
            return false;
        }
        return true;
    }
}
